﻿/**********************************************************
*-----------------------------------*
* flareJ JavaScript Library
*-----------------------------------*
* 作者:Joe_Sky
* 邮箱:hitomi_doax@tom.com
* 最后更新时间:2011-8-23
*-----*-----*-----*-----*-----*-----*
* 基于jQuery
***********************************************************/

/*----------------------------------------------*
* flareJ.Validate
*-------*-------*-------*-------*-------*-------*
* 表单验证
*-------*-------*-------*-------*-------*-------*
* (必须)需引入flareJ.Base.js
* (可选)浮动提示需引入flareJ.ToolTip.js
*----------------------------------------------*/
(function ($) {
    var FJ = $.flareJ, flareJ = FJ;

    /************************************************************
    *-----------------------------------------------------------*
    *                          表单验证
    *-----------------------------------------------------------*
    *************************************************************
    *-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*
    * 由史文星的jquery.RegUser.js修改而成
    *-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*
    * 调用方法:$("#textbox").VAJ();
    ************************************************************/
    this.ValidateJ = this.VAJ = FJ.ValidateJ = FJ.VAJ = FJ.CJ.extend({
        //#region 构造方法
        init: function (elemObj, settings) {
            //参数
            this._super(elemObj, $.extend(true, {
                fjType: "VAJ",
                renderTo: "body",
                regType: "Empty",                         /*-------*验证方式*-------*
                                                          * Empty:非空
                                                          * Equal:验证相等(如密码确认)
                                                          * Exp:正则表达式
                                                          * Custom:自定义验证
                                                          *-----*-----*-----*-----*/
                input: elemObj,                           //验证文本框
                inputE: null,                             //验证相等的文本框
                hasTxtErrorColor: true,                   //是否有错误文本框颜色
                txtBorderWidth: 1,                        //文本框边宽
                isShowTrue: false,                        //是否显示正确图标
                valElem: null,                            //验证信息显示元素
                regMsg: "不能为空!",                      //验证提示信息
                regExp: null,                             //验证正则表达式
                allowBlank: null,                         //是否允许留空
                regFn: null,                              //自定义验证方法
                showMsg: true,                            //是否显示提示信息
                isInline: false,                           //提示信息是否换行
                msgShift: { t: 0, l:0 },                  //错误信息位置补正值
                msgDirect: "bottom",                      //提示位置方向
                group: null,                              //验证组
                showType: "float",                        /*-------*显示方式*-------*
                                                           * normal:显示在元素后面或指定位置
                                                           * float:浮动
                                                           *-----*-----*-----*-----*/
                widthF: "auto",                           //最大宽度
                heightF: "auto",                          //最大高度
                borderWidth: 0,
                colorParams: {
                    bgColor: "transparent",                     //背景色
                    trueColor: "green",                          //正确信息颜色
                    errorColor: "red",                           //错误信息颜色
                    txtErrorColor: "red",                     //错误文本框颜色
                    txtColor: "#8EB5D4",                      //文本框颜色
                    ttBgColor: "#FEF6E1",
                    ttBorderColor: "#0f7cad"
                }
            }, settings));

            this.initFn();

            return this;
        },
        //#endregion

        //#region 初始化
        initFn: function () {
            //将验证相等的文本框转化为jquery对象
            this.p.inputE = (typeof this.p.inputE == 'string' ? $("#" + this.p.inputE) : this.p.inputE);
            this.p.valElem = (this.p.valElem != null ? (typeof this.p.valElem == 'string' ? $("#" + this.p.valElem) : this.p.valElem) : null);

            this._super();
            this.create();

            //加入验证组
            if(this.p.group) {
                if(!VAJ.group[this.p.group])
                    VAJ.group[this.p.group] = [];
                VAJ.group[this.p.group].push(this);
            }
        },
        //#endregion

        //#region 构建
        create: function () {
            this._super();

            //外层
            this.divOut.attr("id", "VAJ_" + this.objId).css("font-family", "宋体");

            //绑定到文本框
            this.bindInput();

            return this;
        },
        //#endregion

        //#region 绑定到文本框
        bindInput: function () {
            var thiz = this;
            switch (this.p.regType) {
                case "Empty":
                case "Equal":
                case "Exp":
                case "Custom":
                    this.p.input.bind("blur", function (e) {
                        thiz.reg();
                    });

                    if(this.p.showType == "normal"){
                        if(!this.p.valElem)
                            this.p.input.after(this.divOut);
                        else
                            this.p.valElem.append(this.divOut);
                    }
                    else if(this.p.showType == "float"){   //浮动
                        this.ttj = $(this.p.input).TTJ({
                            renderTo: this.p.renderTo,
                            hoverDirect: this.p.msgDirect,
                            shiftLeft: this.p.msgShift.l,
                            shiftTop: this.p.msgShift.t,
                            radius: 3,
                            hoverName: "ttj_" + this.objId,
                            hoverType: "free",
                            speed: 1,
                            showSpeed: 200,
                            widthF: 150,
                            heightF: 30,
                            isAsync: false,
                            urlType: "html",
                            onlyFirstLoad: true,
                            colorParams: {
                                borderOut: this.p.colorParams.ttBorderColor,
                                bgColorBody: this.p.colorParams.ttBgColor,
                                bgColorBodyB: this.p.colorParams.ttBgColor
                            },
                            evts: {
                                alferrender: function(e, p){
                                    this.bodyIn.css({
                                        wordWrap:"break-word",
                                        overflow:"hidden"
                                    });
                                },
                                afterbodyload: function(e, p){
                                    this.divOut.css("height", "auto");
                                    this._w = this.divOut.width() + 5;
                                    this._h = this.divOut.height() + 5;
                                    this.divOut.width(this._w);
                                    this.divOut.height(this._h);
                                },
                                aftershow: function(e, p){
                                    if(this._w != null && this._h != null){
                                        this.divOut.width(this._w);
                                        this.divOut.height(this._h);
                                    }
                                }
                            }
                        });
                    }
                    break;
            }

            return this;
        },
        //#endregion

        //#region 显示信息
        showInfo: function(isTrue){
            var thiz = this;
            var info;
            if(isTrue){
                info = $("<div style='margin-left:5px;'>√</div>");
            }
            else{
                info = $("<div style='margin-left:5px;'><span><strong>×</strong></span>" + this.p.regMsg + "</div>");
            }

            if(this.p.showType == "normal"){
                //设置圆角
                var radius;
                if (FJ.isFF) {
                    if (!FJ.isFF4up)
                        radius = "-moz-border-radius";
                    else
                        radius = "border-radius";
                }
                else if (FJ.isWebkit) {
                    radius = "-webkit-border-radius";
                }
                else if (FJ.isOpera || FJ.isIE9) {
                    radius = "border-radius";
                }
                if (radius){
                    this.divOut.css(radius, 3);
                }
                this.divOut.css({
                    width: 150,
                    padding: 5,
                    position: "relative",
                    zIndex: 100,
                    backgroundColor: this.p.colorParams.ttBgColor,
                    border: "1px solid " + this.p.colorParams.ttBorderColor
                });

                //显示
                if(this.p.isInline){
                    this.divOut.css({
                        display: "inline",
                        marginTop: this.p.msgShift.t,
                        marginLeft: this.p.msgShift.l
                    }).html(info);
                }
                else{
                    this.divOut.show().css({
                        marginTop: this.p.msgShift.t,
                        marginLeft: this.p.msgShift.l
                    }).html(info);
                }
            }
            else if(this.p.showType == "float"){   //浮动
                this.ttj.p.fnHtml = function(){
                    return info.css({
                        margin: "3px 0 0 3px",
                        color: thiz.p.colorParams.errorColor
                    });
                };
                this.ttj.setVisible(true);
            }
        },
        //#endregion

        //#region 验证
        reg: function () {
            var regTrue = false;
            switch (this.p.regType) {
                case "Empty":
                    regTrue = $.trim(this.p.input.val()) != "";
                    break;
                case "Equal":
                    regTrue = $.trim(this.p.input.val()) == $.trim(this.p.inputE.val());
                    break;
                case "Exp":
                    var regEp = FJ.RX.test(this.p.regExp, $.trim(this.p.input.val()));
                    if (this.p.allowBlank)
                        regTrue = regEp || $.trim(this.p.input.val()) == "";
                    else
                        regTrue = regEp;
                    break;
                case "Custom":
                    var regEp = false, input = $.trim(this.p.input.val());
                    if (this.p.regFn)
                        regEp = this.p.regFn.call(this, input);
                    if (this.p.allowBlank)
                        regTrue = regEp || input == "";
                    else
                        regTrue = regEp;
                    break;
            }

            //#region 显示提示信息

            if(this.p.showType == "normal"){
                this.divOut.hide();
            }
            else if(this.p.showType == "float"){   //浮动
                this.ttj.setVisible(false);
            }

            if (regTrue) {
                if(this.p.hasTxtErrorColor){
                    this.p.input.css({
                        borderWidth: this.p.txtBorderWidth,
                        borderColor: this.p.colorParams.txtColor,
                        borderStyle: "solid"
                    });
                }

                if(this.p.isShowTrue){
                    this.divOut.css({
                        color: this.p.colorParams.trueColor,
                        fontSize: "13px",
                        fontWeight: "bold"
                    });

                    this.showInfo(true);
                }
            }
            else {
                if(this.p.hasTxtErrorColor){
                    this.p.input.css({
                        borderWidth: this.p.txtBorderWidth,
                        borderColor: this.p.colorParams.txtErrorColor,
                        borderStyle: "solid"
                    });
                }

                this.divOut.css({
                    color: this.p.colorParams.errorColor,
                    fontSize: "12px",
                    fontWeight: "normal"
                });

                this.showInfo(false);
            }
            //#endregion

            return regTrue;
        }
        //#endregion
    });

    //#region 验证组
    FJ.VAJ.group = {};

    //按组名清除验证组
    FJ.VAJ.clearGroup = function(group) {
        if(!VAJ.group[group])
            delete VAJ.group[group];
    };

    //验证
    FJ.VAJ.reg = function(group) {
        var arr = VAJ.group[group];
        if(arr && arr.length > 0){
            var b = true;
            for(var i = 0;i < arr.length;i++){
                if(!arr[i].reg()){
                    b = false;
                }
            }
            return b;
        }
        else{
            return false;
        }
    };
    //#endregion

    //#region 绑定到jquery
    $.fn.extend({
        ValidateJ: function (settings) {
            return new FJ.ValidateJ(this, settings);
        },
        VAJ: function (settings) {
            return $(this).ValidateJ(settings);
        }
    });
    //#endregion

})(jQuery);